package tencent

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	return add(l1, l2, 0)
}

func add(l1 *ListNode, l2 *ListNode, carry int) *ListNode {
	if l1 == nil && l2 == nil {
		if carry == 0 {
			return nil
		}
		return &ListNode{Val: carry}
	}
	var v1, v2 int
	if l1 != nil {
		v1 = l1.Val
		l1 = l1.Next
	}
	if l2 != nil {
		v2 = l2.Val
		l2 = l2.Next
	}
	head := &ListNode{
		Val: (v1 + v2 + carry) % 10,
	}
	next := add(l1, l2, (v1+v2+carry)/10)
	head.Next = next
	return head
}
